<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Git,">










<meta name="description" content="本地提交工作目录和暂存区新建的文件不会被 Git 自动跟踪，需要手动 git add 加入工作目录 已经提交过的文件有修改会被 Git 自动跟踪，但不会加入暂存区，也需要手动 git add 加入暂存区">
<meta name="keywords" content="Git">
<meta property="og:type" content="article">
<meta property="og:title" content="Git tutor">
<meta property="og:url" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/index.html">
<meta property="og:site_name" content="Yu&#39;s Home">
<meta property="og:description" content="本地提交工作目录和暂存区新建的文件不会被 Git 自动跟踪，需要手动 git add 加入工作目录 已经提交过的文件有修改会被 Git 自动跟踪，但不会加入暂存区，也需要手动 git add 加入暂存区">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102114907620.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102143735782.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102143745993.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102143813452.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102162728484.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102162145482.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102162159847.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102145202564.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/1771B44A-26D8-4726-B1E6-010692F198E9.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102173026141.png">
<meta property="og:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102174608558.png">
<meta property="og:updated_time" content="2023-02-25T04:29:10.117Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Git tutor">
<meta name="twitter:description" content="本地提交工作目录和暂存区新建的文件不会被 Git 自动跟踪，需要手动 git add 加入工作目录 已经提交过的文件有修改会被 Git 自动跟踪，但不会加入暂存区，也需要手动 git add 加入暂存区">
<meta name="twitter:image" content="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/image-20211102114907620.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"hide","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '9PFXHHRD2G',
      apiKey: '45293228d8a5cecc1c0826fb3d34a130',
      indexName: 'blog',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"没有找到任何结果: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/">





  <title>Git tutor | Yu's Home</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1746d8de4fb78edd21f9004d348fa6be";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Yu's Home</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">管它真理无穷</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于Yu
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Yu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/xiaomai.png">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Yu's Home">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Git tutor</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-03-20T12:23:55+08:00">
                2022-03-20
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
              <i class="fa fa-comment-o"></i>
              </span>
              <a href="/2022/03/20/Git-tutor/#comments" itemprop="discussionUrl">
              <span class="post-comments-count valine-comment-count" data-xid="/2022/03/20/Git-tutor/" itemprop="commentCount"></span>
              </a>
              </span>
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  1,776
                </span>
              

              

              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="本地提交"><a href="#本地提交" class="headerlink" title="本地提交"></a>本地提交</h2><h3 id="工作目录和暂存区"><a href="#工作目录和暂存区" class="headerlink" title="工作目录和暂存区"></a>工作目录和暂存区</h3><p>新建的文件不会被 Git 自动跟踪，需要手动 <code>git add</code> 加入工作目录</p>
<p>已经提交过的文件有修改会被 Git 自动跟踪，但不会加入暂存区，也需要手动 <code>git add</code> 加入暂存区</p>
<a id="more"></a>

<img src="/2022/03/20/Git-tutor/image-20211102114907620.png" alt="1678a520a2eb7ace2042568cb23babd3" style="zoom:50%;">

<p>加入暂存区的文件<strong>变化</strong>，<code>git commit</code> 成功会创建提交</p>
<blockquote>
<p>平时在开发过程中，修改的文件居多，如果一个个 add 再提交会非常麻烦，这时候可以<code>git commit -am &quot;&quot;</code>进行提交，会将修改的文件自动加入到暂存区中提交，而且不会带上未跟踪的文件</p>
</blockquote>
<h2 id="pull和push"><a href="#pull和push" class="headerlink" title="pull和push"></a>pull和push</h2><h3 id="pull"><a href="#pull" class="headerlink" title="pull"></a>pull</h3><p><code>git pull</code> == <code>git fetch</code> + <code>git merge</code></p>
<p><code>git fetch</code> 会更新所有的 remote tracking，注意并不会更新本地分支</p>
<img src="/2022/03/20/Git-tutor/image-20211102143735782.png" alt="d7ee722f250c4a89b83cc4f41888841b" style="zoom:67%;">

<p><code>git merge</code> 将创建合并请求更新当前HEAD</p>
<img src="/2022/03/20/Git-tutor/image-20211102143745993.png" alt="02a454f78ad3926dcc8de3db9248229a" style="zoom:80%;">

<blockquote>
<p>pull 默认以 <code>git merge</code> 的方式进行合并，也可以指定 <code>--rebase</code> 参数用 <code>git rebase</code> 进行合并</p>
</blockquote>
<h3 id="push"><a href="#push" class="headerlink" title="push"></a>push</h3><p>push 用于将本地代码推送到中心存储，相当于反向 pull，只不过中心存储的服务器不会自动 merge，只接受向前的推送，所以通常需要先本地 pull 再 push。</p>
<img src="/2022/03/20/Git-tutor/image-20211102143813452.png" alt="e393cb2321f05d37bf1c75beddf8a008" style="zoom:67%;">

<p>本地新建的分支在推送时需要与远程关联，通常的写法会是：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">// Under new git version(old version need add -u)</span><br><span class="line">git push origin master</span><br><span class="line">// equal to</span><br><span class="line">git push --set-upstream origin master</span><br><span class="line">// equal to</span><br><span class="line">git branch --set-upstream master origin/master</span><br><span class="line">git push origin master</span><br></pre></td></tr></table></figure>

<p>在推送的时候需要手写分支名是件挺麻烦的事，这时候可以用 HEAD 替换 master 进行推送：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin HEAD</span><br></pre></td></tr></table></figure>

<blockquote>
<p>对于强制推送，建议禁止在非个人分支下操作。并且如果个人分支需要重写的commit已经合入到了其他分支，不建议重写</p>
</blockquote>
<h2 id="合并"><a href="#合并" class="headerlink" title="合并"></a>合并</h2><h3 id="merge"><a href="#merge" class="headerlink" title="merge"></a>merge</h3><h4 id="Fast-forward"><a href="#Fast-forward" class="headerlink" title="Fast-forward"></a>Fast-forward</h4><img src="/2022/03/20/Git-tutor/image-20211102162728484.png" alt="78c83d5b77ca599e17281245c7858e87" style="zoom:67%;">

<blockquote>
<p>在供应链的分支规范当中，单人开发也需要个人分支和主分支，Gitlab的合并规则为Three-way merge，可以在本地模拟Gitlab MR进行合并节省时间：<code>git merge --no-ff &lt;branch&gt;</code></p>
</blockquote>
<h4 id="Three-way"><a href="#Three-way" class="headerlink" title="Three-way"></a>Three-way</h4><img src="/2022/03/20/Git-tutor/image-20211102162145482.png" alt="ad7911c78220d0822d28b43e80ec9d89" style="zoom:67%;">

<h3 id="rebase"><a href="#rebase" class="headerlink" title="rebase"></a>rebase</h3><img src="/2022/03/20/Git-tutor/image-20211102162159847.png" alt="4986d858363d8b79e388d3655420e6d2" style="zoom:67%;">

<h2 id="代码回退"><a href="#代码回退" class="headerlink" title="代码回退"></a>代码回退</h2><h3 id="revert反做"><a href="#revert反做" class="headerlink" title="revert反做"></a>revert反做</h3><p>Git 中的 commit 记录的是文件的变更，反做可以去掉 commit 带来的文件变更影响。</p>
<h4 id="场景一：历史的一个commit删错了文件"><a href="#场景一：历史的一个commit删错了文件" class="headerlink" title="场景一：历史的一个commit删错了文件"></a>场景一：历史的一个commit删错了文件</h4><p>例如 C1 时我们不小心删除了错误的配置文件，而后面的代码又有许多 feat 提交，这时候我们不用切换到其他分支拷贝文件再提交，只需要 <code>git revert C1</code> 即可</p>
<img src="/2022/03/20/Git-tutor/image-20211102145202564.png" alt="2c09faeac2dc28ce76d36519ca573356" style="zoom:67%;">

<h4 id="场景二：合错代码"><a href="#场景二：合错代码" class="headerlink" title="场景二：合错代码"></a>场景二：合错代码</h4><p>如果刚合入，我们可以使用 reset 回退这次合入：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard HEAD~</span><br></pre></td></tr></table></figure>

<p>但如果代码合入已经有一段时间了，尤其是一些主干分支的错误合入，在发现合错期间已经有很多开发分支从主干中切出了，这时候使用reset 回退主干，又会产生回合重新引入的问题。这时候 revert 就更适合：</p>
<p><strong>如果是 Fast-foward 合入</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git revert &lt;commit_id1&gt;..&lt;commit_id2&gt;</span><br></pre></td></tr></table></figure>

<p><strong>如果是 Three-way 合入</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git revert &lt;commit_id&gt; -m 1</span><br></pre></td></tr></table></figure>

<p>其中 -m 表示对 mr 进行 revert 操作；1表示parent_id（合入的一方），通常是基线一方，用于反转指定相对于parent的更改</p>
<h3 id="reset回退"><a href="#reset回退" class="headerlink" title="reset回退"></a>reset回退</h3><p>reset通常用于代码回退，也就是回退 commit，分为三种模式 soft / mixed / hard</p>
<img src="/2022/03/20/Git-tutor/1771B44A-26D8-4726-B1E6-010692F198E9.png" alt="33e99af6f87aa60f937e0f253d3a34ca" style="zoom:67%;">


<h4 id="场景一：历史的一个commit删错了文件-1"><a href="#场景一：历史的一个commit删错了文件-1" class="headerlink" title="场景一：历史的一个commit删错了文件"></a>场景一：历史的一个commit删错了文件</h4><p>这个场景相比 revert 的一致，但如果那一次 commit 中还包含了其他文件的提交，用 revert 会将其他代码一同 revert，这时候可以用 reset 来解决这个问题。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 注意是目标commit的前一个commit</span><br><span class="line">git reset --mixed C0 &lt;deleted_file&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> if modified file, my be need `git checkout &lt;file&gt;`</span><br><span class="line">git commit -am "revert xxx"</span><br></pre></td></tr></table></figure>

<h4 id="场景二：回退和撤销回退"><a href="#场景二：回退和撤销回退" class="headerlink" title="场景二：回退和撤销回退"></a>场景二：回退和撤销回退</h4><p>假如自己引入了一组无用提交，想重置到没有提交之前的状态：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git log</span><br><span class="line"><span class="meta">#</span> 找到要回退的commit</span><br><span class="line">git reset --hard &lt;commit&gt;</span><br></pre></td></tr></table></figure>

<p>重置完之后，又发现自己原来的代码里面有一些代码要用…这时候 git log 已经看不到自己的提交了，这时候就需要配合 reflog 来解决了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git reflog</span><br><span class="line"><span class="meta">#</span> 找到回退之前的commit后</span><br><span class="line">git reset --hard &lt;commit&gt;</span><br></pre></td></tr></table></figure>

<h2 id="重写历史"><a href="#重写历史" class="headerlink" title="重写历史"></a>重写历史</h2><h3 id="rebase变基"><a href="#rebase变基" class="headerlink" title="rebase变基"></a>rebase变基</h3><p>在开发过程中，很有可能在提交一个 commit 后发现之前的 commit 粒度太小太多了，也有可能 commit graph 像 reformat1, fix conflict 1, fix conflict 2…  这时候如果能合并这几次提交就好看多了，以 rebase 交互模式( -i )合并最近两次提交为例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git rebase -i HEAD~2</span><br></pre></td></tr></table></figure>

<img src="/2022/03/20/Git-tutor/image-20211102173026141.png" alt="8a3a378b613ae4595fbec3a3ddeb2ce8" style="zoom:67%;">

<h3 id="commit-–amend"><a href="#commit-–amend" class="headerlink" title="commit –amend"></a>commit –amend</h3><p>commit –amend 可以添加当前暂存区的内容到最后一次提交，并可以修改 commit message。</p>
<p>比如发现最近的 commit 漏提交了文件：</p>
<img src="/2022/03/20/Git-tutor/image-20211102174608558.png" alt="7cc4504c9618389c4e377616a24c3b0a" style="zoom:67%;">

<h2 id="冲突解决"><a href="#冲突解决" class="headerlink" title="冲突解决"></a>冲突解决</h2><p>两个人在各自的行进过程修改了同一文件中的同一行代码，就会产生冲突。</p>
<p>Git 通过 <a href="https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/" target="_blank" rel="noopener">Myers 算法</a> 找到引起冲突的最小变化并标记。</p>
<blockquote>
<p> 在解决冲突应当找到相关代码的成员面对面一起解，而且解冲突是少数建议用可视化工具进行操作的，因为文本标记解决冲突非常不友好。</p>
</blockquote>
<h2 id="其他实用小技巧"><a href="#其他实用小技巧" class="headerlink" title="其他实用小技巧"></a>其他实用小技巧</h2><h3 id="gitignore"><a href="#gitignore" class="headerlink" title="gitignore"></a>gitignore</h3><p><code>.gitignore</code> 文件放置在根目录下，可以在提交时忽略匹配的文件，各种工程和语言的 .gitignore 模板建议在 <a href="http://www.gitignore.io/" target="_blank" rel="noopener">gitignore.io</a> 查找</p>
<p><strong>关于设置全局 gitignore</strong></p>
<p>有时候编码过程中会产生很多个人文件，例如每个人会习惯生成不同名称的二进制，验证文件，中间代码等…</p>
<p>这时候可以在 <code>~/.gitconfig</code> 下配置全局的 .gitignore 文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[core]</span><br><span class="line">    Xcludesfile = ~/.gitignore_global</span><br></pre></td></tr></table></figure>

<p><strong>删除添加 <code>.gitignore</code>文件前错误提交的文件</strong></p>
<p>通常很多时候是发现了不想被提交的文件之后才会添加 <code>.gitignore</code> ，这时候需要重新回到工作区再加入到暂存区：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git rm -r --cached .</span><br><span class="line">git add .</span><br><span class="line">git commit -m "remove xxx"</span><br></pre></td></tr></table></figure>

<h3 id="git-checkout-妙用"><a href="#git-checkout-妙用" class="headerlink" title="git checkout 妙用"></a>git checkout 妙用</h3><p><strong>回退到上一次切换的分支</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -</span><br></pre></td></tr></table></figure>

<blockquote>
<p>也可以用在 git merge -  会合并上一次切换的分支</p>
</blockquote>
<p><strong>放弃工作区的提交</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 放弃所有未暂存的提交</span><br><span class="line">git checkout -- *</span><br><span class="line">git checkout .</span><br><span class="line"><span class="meta">#</span> 放弃某个文件提交</span><br><span class="line">git checkout -- &lt;filename&gt;</span><br></pre></td></tr></table></figure>

<p><strong>放弃暂存区的提交</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 放弃所有暂存区的提交</span><br><span class="line">git reset</span><br><span class="line"><span class="meta">#</span> 放弃某个文件在暂存区的提交</span><br><span class="line">git reset &lt;filename&gt;</span><br></pre></td></tr></table></figure>

<h3 id="git-hooks"><a href="#git-hooks" class="headerlink" title="git hooks"></a>git hooks</h3><p>Git 的 hooks 能够在 commit / merge / checkout 等等阶段调用钩子事件，广泛用于代码提交前的代码审批，CI等等</p>
<ul>
<li><a href="https://github.com/pre-commit/pre-commit" target="_blank" rel="noopener">pre-commit</a></li>
<li><a href="https://github.com/typicode/husky" target="_blank" rel="noopener">husky</a></li>
</ul>
<h3 id="清理历史文件"><a href="#清理历史文件" class="headerlink" title="清理历史文件"></a>清理历史文件</h3><p>Git 开发过程中可能经常会不小心推送上大的二进制文件，密钥文件等。 这时候就需要重写存储树中的提交，而不是仅仅删除文件后重新commit</p>
<blockquote>
<p>Refer: <a href="https://github.com/newren/git-filter-repo" target="_blank" rel="noopener">https://github.com/newren/git-filter-repo</a></p>
</blockquote>
<h3 id="Tutorials"><a href="#Tutorials" class="headerlink" title="Tutorials"></a>Tutorials</h3><ul>
<li>Git reset 三种模式  <a href="https://www.jianshu.com/p/c2ec5f06cf1a" target="_blank" rel="noopener">https://www.jianshu.com/p/c2ec5f06cf1a</a></li>
<li>Atlassian git tutorials  <a href="https://www.atlassian.com/git/tutorials" target="_blank" rel="noopener">https://www.atlassian.com/git/tutorials</a></li>
<li>learngitbranching.js  <a href="https://learngitbranching.js.org/?NODEMO=&amp;locale=en_US" target="_blank" rel="noopener">https://learngitbranching.js.org/?NODEMO=&amp;locale=en_US</a></li>
<li>Cheat-sheet  <a href="https://github.com/arslanbilal/git-cheat-sheet/blob/master/other-sheets/git-cheat-sheet-zh.md" target="_blank" rel="noopener">https://github.com/arslanbilal/git-cheat-sheet/blob/master/other-sheets/git-cheat-sheet-zh.md</a></li>
<li>ProGit  <a href="https://www.progit.cn" target="_blank" rel="noopener">https://www.progit.cn</a></li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>觉得好的话就打赏Ta一瓶冰阔落吧</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.jpg" alt="Yu 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="Yu 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    Yu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://yujunjiex.gitee.io/2022/03/20/Git-tutor/" title="Git tutor">https://yujunjiex.gitee.io/2022/03/20/Git-tutor/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Git/" rel="tag"># Git</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/11/10/Golang覆盖率实践/" rel="next" title="Golang覆盖率实践">
                <i class="fa fa-chevron-left"></i> Golang覆盖率实践
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2022/05/25/浅谈分库分表/" rel="prev" title="浅谈分库分表">
                浅谈分库分表 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<div class="addthis_inline_share_toolbox">
  <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5b03d4a278f58ab9" async="async"></script>
</div>

      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/xiaomai.png" alt="Yu">
            
              <p class="site-author-name" itemprop="name">Yu</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">28</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">30</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yujunjiex" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#本地提交"><span class="nav-number">1.</span> <span class="nav-text">本地提交</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#工作目录和暂存区"><span class="nav-number">1.1.</span> <span class="nav-text">工作目录和暂存区</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#pull和push"><span class="nav-number">2.</span> <span class="nav-text">pull和push</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#pull"><span class="nav-number">2.1.</span> <span class="nav-text">pull</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#push"><span class="nav-number">2.2.</span> <span class="nav-text">push</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#合并"><span class="nav-number">3.</span> <span class="nav-text">合并</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#merge"><span class="nav-number">3.1.</span> <span class="nav-text">merge</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Fast-forward"><span class="nav-number">3.1.1.</span> <span class="nav-text">Fast-forward</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Three-way"><span class="nav-number">3.1.2.</span> <span class="nav-text">Three-way</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rebase"><span class="nav-number">3.2.</span> <span class="nav-text">rebase</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码回退"><span class="nav-number">4.</span> <span class="nav-text">代码回退</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#revert反做"><span class="nav-number">4.1.</span> <span class="nav-text">revert反做</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#场景一：历史的一个commit删错了文件"><span class="nav-number">4.1.1.</span> <span class="nav-text">场景一：历史的一个commit删错了文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#场景二：合错代码"><span class="nav-number">4.1.2.</span> <span class="nav-text">场景二：合错代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#reset回退"><span class="nav-number">4.2.</span> <span class="nav-text">reset回退</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#场景一：历史的一个commit删错了文件-1"><span class="nav-number">4.2.1.</span> <span class="nav-text">场景一：历史的一个commit删错了文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#场景二：回退和撤销回退"><span class="nav-number">4.2.2.</span> <span class="nav-text">场景二：回退和撤销回退</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#重写历史"><span class="nav-number">5.</span> <span class="nav-text">重写历史</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#rebase变基"><span class="nav-number">5.1.</span> <span class="nav-text">rebase变基</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#commit-–amend"><span class="nav-number">5.2.</span> <span class="nav-text">commit –amend</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#冲突解决"><span class="nav-number">6.</span> <span class="nav-text">冲突解决</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#其他实用小技巧"><span class="nav-number">7.</span> <span class="nav-text">其他实用小技巧</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#gitignore"><span class="nav-number">7.1.</span> <span class="nav-text">gitignore</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#git-checkout-妙用"><span class="nav-number">7.2.</span> <span class="nav-text">git checkout 妙用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#git-hooks"><span class="nav-number">7.3.</span> <span class="nav-text">git hooks</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#清理历史文件"><span class="nav-number">7.4.</span> <span class="nav-text">清理历史文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Tutorials"><span class="nav-number">7.5.</span> <span class="nav-text">Tutorials</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2018 &mdash; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heartbeat"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Yu</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">46.7k</span>
  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a></div>



        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>

<script type="text/javascript" src="/custom/fold_action.js?v="></script>





  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//code.bdstatic.com/npm/leancloud-storage@4.12.0/dist/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'rg0EMRYKYKMhOh1zwqEKS5jz-gzGzoHsz',
        appKey: 'T04zQc0OzQd1uxOqcVomoGqj',
        placeholder: '(= =)~*只有10086嘘寒问暖',
        avatar:'mm',
        guest_info:guest,
        pageSize:'5' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  


  

  

  
  

  

  

  


</body>
</html>

